package xuegao.study.mall.oauth2.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.constant.AuthConstant;
import xuegao.study.mall.oauth2.domain.Oauth2TokenDto;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "AuthController")
@Tag(name = "AuthController", description = "认证授权管理接口")
@Controller
@RequestMapping("/oauth")
public class AuthController {

    @Autowired
    private TokenEndpoint tokenEndpoint;

    @ApiOperation("oauth2获取token")
    @PostMapping(value = "token")
    @ResponseBody
    public CommonResult<Oauth2TokenDto> postAccessToken(HttpServletRequest request,
                                                        @RequestParam String grant_type,
                                                        @RequestParam String client_id,
                                                        @RequestParam String client_secret,
                                                        @RequestParam(required = false) String code,
                                                        @RequestParam(required = false) String redirect_uri,
                                                        @RequestParam(required = false) String refresh_token,
                                                        @RequestParam(required = false) String username,
                                                        @RequestParam(required = false) String password) throws HttpRequestMethodNotSupportedException {
        //此处的用户来自两部分
        //1.来自从商城前台的会员登录(提供username和password)
        //2.来自后台提供的开放api(提供client_id和client_secret)
        //todo 差出来的参数是调用方提供还是此处提供
        Map<String, String> parameters = new HashMap<>();
        parameters.put("grant_type",grant_type);
        parameters.put("client_id",client_id);
        parameters.put("client_secret",client_secret);
        parameters.put("code", code);
        parameters.putIfAbsent("refresh_token",refresh_token);
        parameters.putIfAbsent("username",username);
        parameters.putIfAbsent("password",password);
        parameters.put("redirect_uri", redirect_uri);
        OAuth2AccessToken oAuth2AccessToken = tokenEndpoint.postAccessToken(request.getUserPrincipal(), parameters).getBody();
        Oauth2TokenDto oauth2TokenDto = Oauth2TokenDto.builder()
                .token(oAuth2AccessToken.getValue())
                .refreshToken(oAuth2AccessToken.getRefreshToken().getValue())
                .expiresIn(oAuth2AccessToken.getExpiresIn())
                .tokenHead(AuthConstant.JWT_TOKEN_PREFIX).build();
        return CommonResult.success(oauth2TokenDto);
    }

}
